package homework.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DBUtil {
	
	private static Connection conn=null;
	
	/**
	 * 获得链接对象
	 * 
	 * @return  获得链接对象
	 */
	public static Connection getConn(){
		
		try {
			//添加数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获得链接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/szrt", "root", "root");
			
		} catch (Exception e) {
		
			e.printStackTrace();
		}	
		
		return conn;
	}
	
	
	/**
	 * 释放资源
	 * @param rs
	 * @param stmt
	 */
	public static void  release(ResultSet rs,PreparedStatement stmt){
		try {
			if(rs!=null){
				rs.close();
			}if(stmt!=null){
				stmt.close();
			}if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	//获得执行sql的对象
	private static PreparedStatement stmt;
	public static PreparedStatement getstmt(String sql){
		
		try {
			//添加数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获得链接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/szrt", "root", "root");
			
			
			stmt= conn.prepareStatement(sql);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}	
		
		//获得执行sql的对象
		return stmt;
	}

}
